package ch.uzh.ifi.seal.ssqa.metrics.heatmap;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ch.uzh.ifi.seal.ssqa.metrics.IssueCountMetric;
import ch.uzh.ifi.seal.ssqa.model.DiagramComponent;

public class IssueTypeHeatMap implements IHeatMap{

	private String name = "Issues";
	
	Date fromDate, toDate;
	String fromDateString, toDateString, type;
	
	
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");

	List<DiagramComponent> components; 
	
	Map<String, Double> resultMap = new HashMap<String, Double>();
	
	private Double minValue, maxValue;
	
	/**
	 * 
	 * @param components
	 * @param startDate --> must be of format yyyy/MM/dd or NULL
	 * @param endDate --> must be of format yyyy/MM/dd or NULL
	 */

	public IssueTypeHeatMap(List<DiagramComponent> components, String startDate, String endDate, String type){
		try {
			this.components = components;
			this.fromDate = (startDate != null) ? this.formatter.parse(startDate) : null;
			this.toDate = (endDate != null) ? this.formatter.parse(endDate) : null;
			this.fromDateString = (startDate != null) ? startDate.replace("/", "-") : null;
			this.toDateString = (endDate != null) ? endDate.replace("/", "-") : null;
			this.type = (type != "") ? type : null;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void calculateMap(){
		for(DiagramComponent component : components){
			IssueCountMetric metric = new IssueCountMetric();
			metric.setTimeInterval(this.fromDateString, this.toDateString);
			metric.setIssueType(type);
			this.resultMap.put(component.getIdentifier(), metric.calculateResult(component).getResult());
		}
		Double[] values = this.resultMap.values().toArray(new Double[0]);
		Arrays.sort(values);
		this.minValue = values[0];
		this.maxValue = values[values.length-1];
	}
	
	@Override
	public Map<String, Double> getHeatMapping() {
		this.calculateMap();
		return this.resultMap;
	}
	
	public Double getMinValue(){
		return this.minValue;
	}
	
	public Double getMaxValue(){
		return this.maxValue;
	}

	@Override
	public String getName() {
		return name;
	}
}
